package com.guoqi.marrydate.flag

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.guoqi.marrydate.DateUtil
import com.guoqi.marrydate.R
import com.guoqi.marrydate.label.LabelView
import java.text.SimpleDateFormat
import java.util.*


/**
 * Created by GUOQI on 2017/12/22.
 */
class EventAdapter(context: Context, beanList: ArrayList<FlagBean>) : CommonAdapter<FlagBean>(context, beanList, R.layout.adapter_event_item) {


    @SuppressLint("SimpleDateFormat")
    override fun convert(holder: ViewHolder, bean: FlagBean) {
        val iv_icon = holder.getView<ImageView>(R.id.iv_icon)
        val tv_title = holder.getView<TextView>(R.id.tv_title)
        val tv_desc = holder.getView<TextView>(R.id.tv_desc)
        val tv_date = holder.getView<TextView>(R.id.tv_date)
        val lb_top = holder.getView<LabelView>(R.id.lb_top)
        val tv_cycle = holder.getView<TextView>(R.id.tv_cycle)

        when (bean.imgRes) {
            0 -> iv_icon.setImageResource(R.mipmap.ic_goal)
            1 -> iv_icon.setImageResource(R.mipmap.ic_eat)
            2 -> iv_icon.setImageResource(R.mipmap.ic_gift)
            3 -> iv_icon.setImageResource(R.mipmap.ic_cake)
            4 -> iv_icon.setImageResource(R.mipmap.ic_idea)
        }
        tv_title.text = bean.title
        tv_desc.text = bean.desc
        tv_date.text = bean.date
        lb_top.visibility = if (bean.sortNo == 0) {
            View.GONE
        } else {
            when (bean.sortNo) {
                50 -> {
                    lb_top.bgColor = Color.parseColor("#00c853")
                    lb_top.text = "重要"
                }
                99 -> {
                    lb_top.bgColor = Color.parseColor("#fb0d1b")
                    lb_top.text = "置顶"
                }
            }
            View.VISIBLE
        }
        if (bean.weekSchedule == 1 || bean.monthSchedule == 1) {
            if (bean.weekSchedule == 1) {
                tv_cycle.text = "每周"
            }
            if (bean.monthSchedule == 1) {
                tv_cycle.text = "每月"
            }
            tv_cycle.visibility = View.VISIBLE
        } else {
            tv_cycle.visibility = View.GONE
        }
        val diff: Int
        try {
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            val futureDate = sdf.parse(bean.date)
            val nowDateZero = System.currentTimeMillis() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().rawOffset
            val diffDays = (futureDate.time - nowDateZero) / (1000 * 3600 * 24)
            diff = diffDays.toInt()
        } catch (e: Exception) {
            e.printStackTrace()
            return
        }

        //每周 每月任务过期后自动添加新任务
        if (diff < 0) {
            if (bean.weekSchedule == 1) {
                bean.date = DateUtil.getNDate(bean.date, 7)
                Log.e("FLAG", "下周日期: ${bean.date}")
                bean.save()
            }
            if (bean.monthSchedule == 1) {
                bean.date = DateUtil.getNextMonth(bean.date)
                Log.e("FLAG", "下月日期: ${bean.date}")
                bean.save()
            }
        }

        if (bean.countType == 0) {
            //倒计时
            when {
                diff < 0 -> {
                    tv_date.text = "已过期"
                    tv_date.setTextColor(Color.parseColor("#CCCCCC"))
                }
                diff == 0 -> {
                    tv_date.text = "今天"
                    tv_date.setTextColor(Color.parseColor("#FF0000"))
                }
                diff == 1 -> {
                    tv_date.text = "明天"
                    tv_date.setTextColor(Color.parseColor("#FF5511"))
                }
                diff == 2 -> {
                    tv_date.text = "后天"
                    tv_date.setTextColor(Color.parseColor("#FF8800"))
                }
                diff > 2 -> {
                    tv_date.text = bean.date
                    tv_date.setTextColor(Color.parseColor("#888888"))
                }
            }
        } else {
            //正计时
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            val date = sdf.parse(bean.date)
            val calendar = Calendar.getInstance()
            calendar.time = date
            tv_date.text = DateUtil.getNatureDiff(Calendar.getInstance(), calendar)
            tv_date.setTextColor(Color.parseColor("#009FCC"))
        }
    }


}